<!DOCTYPE html>
<html>
<head>
<title>科室管理</title>

<meta name="keywords" content="keyword1,keyword2,keyword3">
<meta name="description" content="科室管理页面">
<meta name="content-type" content="text/html; charset=UTF-8">
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

<link rel="stylesheet" href="/Hospital/lib/layui-v2.1.6/layui/css/layui.css" media="all">
<link rel="stylesheet" href="/Hospital/resources/css/table.css" media="all">
<style type="text/css">
</style>
</head>

<body>
	<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
		<legend>科室管理</legend>
		<button id="btn_add" class="layui-btn btn_right">
			<i class="layui-icon">&#xe61f;</i> 添加
		</button>
	</fieldset>
	<table id="dpmTable" class="layui-table table" lay-filter="dpmTable"></table>
</body>

<script src="/Hospital/lib/jquery.min.js" charset="utf-8"></script>
<script src="/Hospital/lib/layui-v2.1.6/layui/layui.js" charset="utf-8"></script>
<script id="operator" type="text/html">
	<a class="layui-btn layui-btn-mini" lay-event="edit">编辑</a>
</script>
<script type="text/javascript">
	var loginUser = JSON.parse(localStorage.getItem("loginUser"));

	layui.use([ "table", "layer","element"], function() {
		var $ = layui.jquery;
		var table = layui.table;
		var element = layui.element;
		var layer = layui.layer;
		
		initFrame();
		
		function initFrame() {
			if(loginUser.permission.createDepartment < 5) {	//管理员才能新增科室
				hide("#btn_add");
			}
			
			initTable(); //初始化表格;
			btnAddEvent();//新增按钮 监听事件
		}
		
		//初始化表格
		function initTable(){
			var height=window.innerHeight*0.8;
			var param = {
				clinicId : loginUser.clinicId,
			};
			
			//管理员获取所有科室;其他获取所属门诊科室
			var url = "";
			if(loginUser.permission.createDepartment == 5) {	//管理员才能新增科室
				url = "/Hospital/clinicController/getAllDepartment";
			}else {
				url = "/Hospital/clinicController/getClinicAllDepartment";
			}
			
			table.render({
				id:'dpmTable',
	            elem: '#dpmTable',//指定原始表格元素选择器（推荐id选择器)
	            page:true, 
	            limit:10,
	            center:true,
	            height:height,
	            cols: createCols(loginUser.permission), //根据是否具有修改门诊权限 生成列
	            url : url,
	            method : "post",
	            where : param,
	            response : {
	               statusName : "code", //数据状态的字段名称
	               statusCode : 200, //成功的状态码
	               countName: 'total', //数据总数的字段名称
	               msgName  : "msg", //状态信息的字段名称
	               dataName : "result" //数据列表的字段名称 
	            }
	         });
	
			//监听工具条 lay-filter
			table.on('tool(dpmTable)', function(obj){
				var data = obj.data;
				//console.log("dpmTable.html/obj.data");
				//console.log(data);
				localStorage.setItem("dpmDetail", JSON.stringify(data));
				
				if(obj.event === 'edit'){
					//科室负责人级别 是否是本科室的负责人
					if(loginUser.permission.alterDepartment == 1 && loginUser.employeeId != data.principalId){
						layer.msg("不是该科室的负责人",{icon:2,time:1000,});
						localStorage.removeItem("dpmDetail");
						return;
					}
					//门诊负责人级别 是否是本科室门诊的负责人
					if(loginUser.permission.alterDepartment == 2 && loginUser.employeeId != data.clinicprincipalId){
						layer.msg("不是该科室的门诊的负责人",{icon:2,time:1000,});
						localStorage.removeItem("dpmDetail");
						return;
					}
					//编辑按钮点击事件
					initAlterFrame(data);
			  	};
			});
		}
		//编辑按钮点击事件
		function initAlterFrame(data) {
			//dpmTable_operator: 1(新增);2(编辑);
			localStorage.setItem("dpmTable_operator", 2);
			layer.open({
				type : 2,
				title : "科室编辑窗口",
				area : ["390px","470px"],
				content : '/Hospital/views/departmentManager/dpmDetail.html',
				btn : ["确认","取消"],
				btnAlign : "r",
				resize : false,
				yes : function(index, layero) {
					var param = $(layero).find("iframe")[0].contentWindow.getParam();
					var mainFrame = index;
					//console.log("dpmTable.html/param");
					//console.log(param);
					//console.log("dpmTable.html/obj.data");
					//console.log(data);
					 //数据验证
					if(!vertify(param)) {
						return;
					}
					 
					//如果没有修改直接显示修改成功
					if(isAlter(param,data)) {
					 	layer.close(mainFrame);
					 	localStorage.removeItem("dpmDetail");
					 	return;
					}
					 
					//插入数据库
					insertToDB(param,mainFrame);
				},
				cancel : function(index, layero) {
					//左上角X关闭按钮
					localStorage.removeItem("dpmDetail");
					layer.close(index);
				},
				btn2 : function(index, layero) {
					//取消按钮
					localStorage.removeItem("dpmDetail");
					layer.close(index);
				}
			});
		}
		
		//新增按钮 监听事件
		function btnAddEvent() {
			$("#btn_add").on("click",function(){
				//dpmTable_operator: 1(新增);2(编辑);
				localStorage.setItem("dpmTable_operator", 1);
				layer.open({
					type : 2,
					title : "创建科室窗口",
					area : ["390px","470px"],
					content : '/Hospital/views/departmentManager/dpmDetail.html',
					btn : ["确认","取消"],
					btnAlign : "r",
					resize : false,
					yes : function(index, layero) {
						var param = $(layero).find("iframe")[0].contentWindow.getParam();
						var mainFrame = index;
						 //数据验证
						if(!vertify(param)) {
							return;
						}
						//插入数据库
						insertToDB(param,mainFrame);
					},
					cancel : function(index, layero) {
						//左上角X关闭按钮
						localStorage.removeItem("dpmDetail");
						layer.close(index);
					},
					btn2 : function(index, layero) {
						//取消按钮
						localStorage.removeItem("dpmDetail");
						layer.close(index);
					}
				});
			});
		}
		
		//插入数据库
		function insertToDB(param,mainFrame){
			var operator = localStorage.getItem("dpmTable_operator");
			if(operator == 1) {
				//新增科室
				layer.confirm('确认创建？', {icon: 3, title:'提示'}, function(index){
					 $.ajax({
						type : "post",
						url : "/Hospital/clinicController/createDepartment",
						data : param,
						success : function(response) {
							if(response.code == 200) {
								layer.msg("创建成功",{icon:1,time:1000});
							}
							var param = {
								clinicId : loginUser.clinicId,
							};
							layer.close(index);
							layer.close(mainFrame);
							var url = "";
							if(loginUser.permission.createDepartment == 5) {	//管理员才能新增科室
								url = "/Hospital/clinicController/getAllDepartment";
							}else {
								url = "/Hospital/clinicController/getClinicAllDepartment";
							}
							table.reload('dpmTable', {
								url : url,
								method : "post",
								where : param,
								response : {
								    statusName : "code", //数据状态的字段名称
								    statusCode : 200, //成功的状态码
								    countName: 'total', //数据总数的字段名称
								    msgName  : "msg", //状态信息的字段名称
								    dataName : "result", //数据列表的字段名称 
								}
							});
						}
					});
				});
			}
			if(operator == 2) {
				//编辑科室
				layer.confirm('确认修改？', {icon: 3, title:'提示'}, function(index){
					 $.ajax({
						type : "post",
						url : "/Hospital/clinicController/alterDepartment",
						data : param,
						success : function(response) {
							if(response.code == 200) {
								layer.msg("修改成功",{icon:1,time:1000});
							}
							var param = {
								clinicId : loginUser.clinicId,
							};
							var url = "";
							if(loginUser.permission.createDepartment == 5) {	//管理员才能新增科室
								url = "/Hospital/clinicController/getAllDepartment";
							}else {
								url = "/Hospital/clinicController/getClinicAllDepartment";
							}
							layer.close(index);
							layer.close(mainFrame);
							table.reload('dpmTable', {
								url : url,
								method : "post",
								where : param,
								response : {
								    statusName : "code", //数据状态的字段名称
								    statusCode : 200, //成功的状态码
								    countName: 'total', //数据总数的字段名称
								    msgName  : "msg", //状态信息的字段名称
								    dataName : "result", //数据列表的字段名称 
								}
							});
						}
					});
				});
			}
		};
	});
	
	//如果没有修改直接显示修改成功
	//param:修改后的值；data:原来的值
	function isAlter(param,data) {
		if(param.phone == data.phone && param.name == data.departmentName 
				&& param.employeeId == data.principalId && param.clinicId == data.clinicId ){
			//console.log("直接修改");
			layer.msg("修改成功",{icon:1,time:1000});
			return true;
		}
		return false;
	}
	
	//TODO 缺少电话
	//验证数据 false:数据不合法;true:数据合法
	function vertify(param) {
		if(param.name == null ||param.name.length == 0){
			 layer.msg("科室名称不能为空",{icon:2,time:1000});
			 return false;
		 }
		 if(param.employeeId == null ||param.employeeId.length == 0){
			 layer.msg("科室负责人有误",{icon:2,time:1000});
			 return false;
		 }
		 return true;
	}
	
	//隐藏块
	function hide(id) {
		$(id).css({
			"display" : "none"
		});
	}
	
	//显示块
	function show(id) {
		$(id).css({
			"display" : "block"
		});
	}
	
	//生成表格的列
	function createCols(permission) {
		//console.log("clinicTable/createCols");
		//没有修改科室权限
		if(permission.alterDepartment == 0) {
			var tableWidth=window.innerWidth*0.141;
			cols = [[
		                {field:'clinicName',width:tableWidth,title:'门诊',align:'center'},
		                {field:'departmentName',width:tableWidth,title:'科室',align:'center'},
		                {field:'creatorName',width:tableWidth,title:'创建人',align:'center'},
		                {field:'principalCode',width:tableWidth,title:'负责人工号',align:'center'},
		                {field:'principalName',width:tableWidth,title:'负责人',align:'center'},
		                {field:'createTime',width:tableWidth,title:'创建时间',align:'center'},
		                {field:'phone',width:tableWidth,title:'联系电话',align:'center'},
		           ]];
			return cols;
		}
		//有修改科室权限 
		var tableWidth=window.innerWidth*0.131;
		var cols = [[
		                {field:'clinicName',width:tableWidth,title:'门诊',align:'center'},
		                {field:'departmentName',width:tableWidth,title:'科室',align:'center'},
		                {field:'creatorName',width:tableWidth,title:'创建人',align:'center'},
		                {field:'principalCode',width:tableWidth,title:'负责人工号',align:'center'},
		                {field:'principalName',width:tableWidth,title:'负责人',align:'center'},
		                {field:'createTime',width:tableWidth,title:'创建时间',align:'center'},
		                {field:'phone',width:tableWidth,title:'联系电话',align:'center'},
		                {toolbar:'#operator',width:70,title:'操作',fixed: 'right'},
		           ]];
		return cols;
	}
</script>
</html>
